package com.hbnu.system.config;

import com.hbnu.system.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 全局跨域与资源配置
 */
@Configuration
@Slf4j
public class GlobalCorsConfig extends WebMvcConfigurationSupport {

    /**
     * 跨域配置
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 注意：Spring Boot 2.4+ 推荐使用 allowedOriginPatterns 替代 allowedOrigins
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(true)
                .maxAge(3600);
        // 无需调用 super.addCorsMappings(registry)，父类方法为空实现
    }

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 注册拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns(
                        // 登录相关路径
                        "/login/login",
                        "/sso/**",
                        // Swagger 2.x（Knife4j）相关路径
                        "/doc.html",                    // 文档首页
                        "/swagger-ui.html",              // Swagger 原生首页（备用）
                        "/webjars/**",                   // 静态资源（JS/CSS）
                        "/v2/api-docs",                  // Swagger 2.x 接口文档数据
                        "/swagger-resources/**",         // 文档资源定义
                        "/swagger-resources",            // 资源列表
                        "/favicon.ico"                   // 网站图标
                );
    }

    /**
     * 静态资源映射（关键：确保 JAR 包内资源可访问）
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射 Swagger 静态资源
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/META-INF/resources/");

        // 若有其他静态资源（如前端页面），需一并映射
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}